package design.service.engine;

import design.model.aggregaets.TreeRich;
import design.model.vo.EngineResult;
import design.model.vo.TreeNode;
import design.model.vo.TreeRoot;
import design.service.logic.LogicFilter;

import java.util.Map;

/**
 * @author: hero生仔
 */
public abstract class EngineBase extends EngineConfig implements IEngine {
    @Override
    public abstract EngineResult process(Long treeId, String userId, TreeRich treeRich, Map<String, String> decisionMatter);

    protected TreeNode engineDecisionMaker(TreeRich treeRich, Long treeId, String userId, Map<String, String> decisionMatter) {
        // 获取决策树根消息和整棵决策树
        TreeRoot treeRoot = treeRich.getTreeRoot();
        Map<Long, TreeNode> treeNodeMap = treeRich.getTreeNodeMap();

        // 规则树根ID
        Long rootNodeId = treeRoot.getTreeRootNodeId();
        TreeNode treeNodeInfo = treeNodeMap.get(rootNodeId);
        // 节点类型【NodeType】：1.子叶、2.果实
        while (treeNodeInfo.getNodeType().equals(1)) {
            // 获取 决策key 从而获取 age 还是 gender 的具体的决策器
            String ruleKey = treeNodeInfo.getRuleKey();
            LogicFilter logicFilter = logicFilterMap.get(ruleKey);
            // 获取具体的决策值
            String matterValue = logicFilter.matterValue(treeId, userId, decisionMatter);
            // 根据决策值获取下一个节点的ID
            Long nextNode = logicFilter.filter(matterValue, treeNodeInfo.getTreeNodeLinkList());
            // 根据ID获取该节点的信息
            treeNodeInfo = treeNodeMap.get(nextNode);
            System.out.println("决策树引擎=>" + treeRoot.getTreeName() + " userId：" + userId + " treeId：" + treeId + " treeNode：" + treeNodeInfo.getTreeNodeId() + " ruleKey：" + ruleKey + "matterValue：" + matterValue);
        }
        // 返回的是【果实】信息
        return treeNodeInfo;
    }
}
